package fag.core.model;

import java.awt.image.BufferedImage;
import java.io.Serializable;

import fag.core.geometry.Rectangle2D;
import fag.core.geometry.Vector2D;

public class Sprite implements Serializable {

	private final static int BOUNDS_SPANNING = 5;
	
	Vector2D offset;
	SerializableImage serializableImage;
	transient BufferedImage bufferedImage;
	
	private static final long serialVersionUID = -8317155833951876104L;
	
	
	public Sprite() {
		
		offset = new Vector2D();
		serializableImage = new SerializableImage();
		bufferedImage = null;
	}
	
	public Sprite(SerializableImage i,Vector2D v) {
	
		offset = v;
		serializableImage = i;
		bufferedImage = null;
	}
	
	public Rectangle2D getBounds() {
		
		Rectangle2D bounds;
		bounds = new Rectangle2D((int)offset.x - BOUNDS_SPANNING,
							     (int)offset.y - BOUNDS_SPANNING,
							     getBufferedImage().getWidth() + BOUNDS_SPANNING * 2,
							     getBufferedImage().getHeight() + BOUNDS_SPANNING * 2);
		return bounds;
	}
	
	public Vector2D getOffSet() {
		
		return offset;
	}
	
	public BufferedImage getBufferedImage() {
		
		if(bufferedImage == null)
			bufferedImage = serializableImage.getBufferedImage();
		
		return bufferedImage;
	}
	
	public void resetBufferedImage() {
		
		bufferedImage = null;
	}
	
	public int getWidth() {
		
		return getBufferedImage().getWidth();
	}
	
	public int getHeight() {
		
		return getBufferedImage().getHeight();
	}
	
	public void setOffset(Vector2D offset) {
		this.offset = offset;
	}
}
